package com.hxgz.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * 阿里云 OSS工具类
 *
 * @author Monkey
 * @version 1.0
 * @date 2017年9月30日下午3:38:09
 */

public class OSSClientUtil {

  public static final Logger logger = LoggerFactory.getLogger(OSSClientUtil.class);
  // endpoint
  private String endpoint = "oss-cn-shanghai.aliyuncs.com";
  // accessKey
  private String accessKeyId = PropertiesUtil.getProperty("accessKeyId");
  private String accessKeySecret = PropertiesUtil.getProperty("accessKeySecret");
  // 空间
  private String bucketName = "kindergarten0";
  // 文件存储目录
  private String filedir = "";

  private OSSClient ossClient;

  public OSSClientUtil() {
    ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  }

  /**
   * 初始化
   */
  public void init() {
    ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  }

  /**
   * 销毁
   */
  public void destory() {
    ossClient.shutdown();
  }

  /*  */

  /**
   * 上传图片
   */
  public void uploadImg2Oss(String url) throws Exception {
    File fileOnServer = new File(url);
    FileInputStream fin;
    try {
      fin = new FileInputStream(fileOnServer);
      String[] split = url.split("/");
      this.uploadFile2OSS(fin, split[split.length - 1]);
    } catch (Exception e) {
      throw new Exception("图片上传失败");
    }
  }

  public String uploadImg2Oss(MultipartFile file) throws Exception {
    if (file.getSize() > 10 * 1024 * 1024) {
      throw new Exception("上传图片大小不能超过10M！");
    }
    String originalFilename = file.getOriginalFilename();
    String substring = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
    Random random = new Random();
    String name = random.nextInt(10000) + System.currentTimeMillis() + substring;
    try {
      InputStream inputStream = file.getInputStream();
      this.uploadFile2OSS(inputStream, name);
      return name;
    } catch (Exception e) {
      throw new Exception("图片上传失败");
    }
  }


  public String uploadImgOutputStream(InputStream inputStream, String format) throws Exception {
    Random random = new Random();
    String name = random.nextInt(10000) + System.currentTimeMillis() + "." + format;
    try {
      this.uploadFile2OSS(inputStream, name);
      return name;
    } catch (Exception e) {
      throw new Exception("图片上传失败");
    }
  }


  /**
   * 获得图片路径
   */
  public String getImgUrl(String fileUrl) {
    System.out.println(fileUrl);
    if (!StringUtils.isEmpty(fileUrl)) {
      String[] split = fileUrl.split("/");
      return this.getUrl(this.filedir + split[split.length - 1]);
    }
    return null;
  }

  /**
   * 上传到OSS服务器 如果同名文件会覆盖服务器上的
   *
   * @param instream 文件流
   * @param fileName 文件名称 包括后缀名
   * @return 出错返回"" ,唯一MD5数字签名
   */
  public String uploadFile2OSS(InputStream instream, String fileName) {
    String ret = "";
    try {
      // 创建上传Object的Metadata
      ObjectMetadata objectMetadata = new ObjectMetadata();
      objectMetadata.setContentLength(instream.available());
      objectMetadata.setCacheControl("no-cache");
      objectMetadata.setHeader("Pragma", "no-cache");
      objectMetadata.setContentType(getcontentType(fileName.substring(fileName.lastIndexOf("."))));
      objectMetadata.setContentDisposition("inline;filename=" + fileName);
      // 上传文件
      PutObjectResult putResult = ossClient
          .putObject(bucketName, filedir + fileName, instream, objectMetadata);
      ret = putResult.getETag();
    } catch (IOException e) {
      logger.error(e.getMessage(), e);
    } finally {
      try {
        if (instream != null) {
          instream.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return ret;
  }

  /**
   * Description: 判断OSS服务文件上传时文件的contentType
   *
   * //         * @param FilenameExtension 文件后缀
   *
   * @return String
   */
  public static String getcontentType(String filenameExtension) {
    if (filenameExtension.equalsIgnoreCase(".bmp")) {
      return "image/bmp";
    }
    if (filenameExtension.equalsIgnoreCase(".gif")) {
      return "image/gif";
    }
    if (filenameExtension.equalsIgnoreCase(".jpeg") || filenameExtension.equalsIgnoreCase(".jpg")
        || filenameExtension.equalsIgnoreCase(".png")) {
      return "image/jpeg";
    }
    if (filenameExtension.equalsIgnoreCase(".html")) {
      return "text/html";
    }
    if (filenameExtension.equalsIgnoreCase(".txt")) {
      return "text/plain";
    }
    if (filenameExtension.equalsIgnoreCase(".vsd")) {
      return "application/vnd.visio";
    }
    if (filenameExtension.equalsIgnoreCase(".pptx") || filenameExtension.equalsIgnoreCase(".ppt")) {
      return "application/vnd.ms-powerpoint";
    }
    if (filenameExtension.equalsIgnoreCase(".docx") || filenameExtension.equalsIgnoreCase(".doc")) {
      return "application/msword";
    }
    if (filenameExtension.equalsIgnoreCase(".xml")) {
      return "text/xml";
    }
    if (filenameExtension.equalsIgnoreCase(".mp4")) {
      return "video/mp4";
    }
    if(filenameExtension.equalsIgnoreCase(".mp3")){
      return "audio/mp3";
    }
//    if (filenameExtension.equalsIgnoreCase(".avi")) {
//      return "video/avi";
//    }
//    if(filenameExtension.equalsIgnoreCase(".movie")){
//      return "video/x-sgi-movie";
//    }
//    if(filenameExtension.equalsIgnoreCase(".mpeg")){
//      return "video/mpg";
//    }
//    if(filenameExtension.equalsIgnoreCase(".mpg")){
//      return "video/mpg";
//    }
//    if(filenameExtension.equalsIgnoreCase(".rmvb")){
//      return "application/vnd.rn-realmedia-vbr";
//    }
//    if(filenameExtension.equalsIgnoreCase(".rm")){
//      return "application/vnd.rn-realmedia";
//    }
//    if(filenameExtension.equalsIgnoreCase(".wmv")){
//      return "video/x-ms-wmv";
//    }
    return "image/jpeg";
  }

  /**
   * 获得url链接
   */
  public String getUrl(String key) {
    // 设置URL过期时间为10年 3600l* 1000*24*365*10
    Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);
    // 生成URL
    URL url = ossClient.generatePresignedUrl(bucketName, key, expiration);
    if (url != null) {
      return url.toString();
    }
    return null;
  }
}

